
$.alert = function(option) {
	var opt = option || {};
	if (typeof option == 'string') {
		opt = {};
		opt.text = option;
	}

	var alertModal = $('#alert-modal')[0];

	alertModal = alertModal ? $(alertModal) : createModal();

	var	cancelBtn = alertModal.find('.cancel-btn'),
		confirmBtn = alertModal.find('.confirm-btn'),
		closeBtn = alertModal.find('.close-modal');

	if (!opt.confirm) {
		opt.confirm = function() {}
	}

	if (!opt.cancel) {
		opt.cancel = function() {}
	}

	// 根据传递的type 判断是否显示cancel btn
	// if (opt.type == 'confirm') {
	// 	cancelBtn.show();
	// } else {
		cancelBtn.hide();
	// }

 	alertModal.removeClass('hide').find('.msg-wrap').text(opt.text);
 
 	confirmBtn.unbind('click').one('click', function() { 
 		confirmCb();
 	})

 	cancelBtn.unbind('click').one('click', function() {
 		cancelCb();
 	})

 	closeBtn.unbind('click').one('click', function() {
 		cancelCb();
 	});


 	// 确认回调
 	function confirmCb() {
 		opt.confirm();
 		alertModal.addClass('hide');
 		$(document).unbind('keyup', keyboardEvent);
 	}

 	// 取消回调
 	function cancelCb() {
 		opt.cancel();
		alertModal.addClass('hide');
		$(document).unbind('keyup', keyboardEvent);
 	}

	function keyboardEvent(e) {
		// 回车事件
		if(event.keyCode == 13) {
	  		confirmCb();
	  	} 

	  	// esc事件
	  	if (event.keyCode == 27) {
	  		cancelCb();
	  	}
	}

 	$(document).bind('keyup', keyboardEvent);

	/**
	 * 创建alert modal
	 * @return {[type]} [description]
	 */
	function createModal() {
		var tem = '<div class="hw-modal" id="alert-modal"><div class="modal-bg"></div><div class="modal-content"><h4 class="modal-title">提示<i class="icon iconfont close-modal">&#xe617;</i></h4><div class="msg-wrap">请至少选择一个产品</div><div class="btn-wrap"><button class="confirm-btn" type="button">确定</button><button class="cancel-btn" type="button">取消</button></div></div></div>';

		return $('body').append(tem).find('#alert-modal');
	}

}



// var tem = '<div class="hw-modal" id="alert-modal"><div class="modal-content"><h4 class="modal-title">提示<span class="close-modal">×</span></h4><div class="msg-wrap">请至少选择一个产品</div><div class="btn-wrap"><button class="confirm-btn" type="button">确定</button></div></div></div>';